-------------------------------------------------------------------------------
--	 File name: init.lua
--		Author: Sploder
-- 		  Date: 02/27/2010
-- Description: This file contains the main body of code used by Sakuya's basic
--					weapon.
--
-------------------------------------------------------------------------------

AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")

SWEP.Weight = 5
SWEP.AutoSwitchTo = true
SWEP.AutoSwitchFrom = true

-- Basic attack variables:
SWEP.shotspeed	= 400				-- This is the speed at which knives are thrown.

SWEP.cooldown	= 0.20				-- This is how long the between each shot can be fired.
SWEP.shoottime	= 0					-- This is the next time a shot can be fired. If a shot can be fired, this will be zero.

SWEP.pressed	= 0					-- Keeps the last time the player pressed attack1.
SWEP.fullcharge	= 0.25				-- Half of the number of seconds before the weapon is fully-charged. (I AM FULLY CHAHRGED!)
SWEP.empty		= 5					-- The number of bullets fired when we aren't charged.
SWEP.full		= 9					-- The number of bullets fired when we are charged.
SWEP.spreadang	= 30				-- The spread of the bullet spray.
SWEP.spreaddis	= 16				-- The distance between each bullet.

SWEP.pPerShot	= 0.10				-- The amount of power the player gets when a shot is fired.
SWEP.pPerHit	= 1					-- The amount of power the player gets when a shot hits.
SWEP.pPerKill	= 5					-- The amount of power the player gets when he scores a kill.

-- Spell card variables:
SWEP.cardActive	= 0					-- Keeps track of which spellcard is active. (0 = no spellcard active)

SWEP.scTimer	= 40				-- The amount of time a spellcard is active.
SWEP.scTimeout	= 0					-- The last time a timeout sound was played.
SWEP.scKills	= 0					-- The number of kills achieved during a spellcard.

SWEP.sc1Shots	= 0					-- Keeps track of the number of shot waves we've fired.
SWEP.sc1MaxShots	= 3				-- The number of shots in a burst.
SWEP.sc1shottime	= 0.20			-- The amount of time between each wave of shots.
SWEP.sc1cooldown	= 0.80			-- The amount of time before the next burst can be fired.

SWEP.sc2cooldown	= 1.00			-- The amount of time before the next burst can be fired.
SWEP.sc2shotspeed	= 64			-- The speed at which the delayed knives will travel (at first).

SWEP.sc3cooldown	= 0.50			-- The amount of time before the next sweep can be fired.
SWEP.sc3timer		= 0				-- The time at which time will be unfrozen again.
SWEP.sc3timertime	= 5.00			-- The amount of time that a time freeze lasts.
SWEP.sc3charge		= 2.00			-- The amount of time that it takes to charge a time freeze.

--SWEP.ItemClass	= "item_wep_sakuya"

-------------------------------------------------------------------------------
--	  Function: SWEP:Initialize()
-- Description: This function initializes the weapon.
-------------------------------------------------------------------------------
function SWEP:Initialize()
	self:SetNetworkedFloat("spiritPower", 0)		-- The amount of spirit power the player has.
	self:SetNetworkedInt("spellcardTimer", 0)		-- The time at which the spellcard runs out.
	self:SetNWInt("scactive", 0)				-- Tells the client-side if spellcard 3 is active or not.
	
	math.randomseed(os.time())						-- Seed random.
	
	self:SetMaterial("models/danmaku/sakuya/rKnife2");
	
	self:SetWeaponHoldType("melee")
	
	self:SetNPCMinBurst(5)
	self:SetNPCMaxBurst(15)
	self:SetNPCFireRate(1)
	self:SetNPCMinRest(2)
	self:SetNPCMaxRest(2)
end

-------------------------------------------------------------------------------
--	  Function: SWEP:Holster()
-- Description: This function is called when the user puts the weapon away.
-------------------------------------------------------------------------------
function SWEP:Holster()
	if (self.shoottime < CurTime())
	then
		self.isactive = false
		return true
	else
		-- Draw the weapon?
		return false
	end
	self:SendWeaponAnim(ACT_VM_HOLSTER);
end

-------------------------------------------------------------------------------
--	  Function: SWEP:Deploy()
-- Description: This function is called when the player draws the weapon.
-------------------------------------------------------------------------------
function SWEP:Deploy()
	self:SendWeaponAnim(ACT_VM_DRAW)
	return true
end

-------------------------------------------------------------------------------
--	  Function: SWEP:Equip( NewOwner )
-- Description: This function is called when the weapon is acquired.
-------------------------------------------------------------------------------
function SWEP:Equip( NewOwner )
	-- It the player has any other weapons, remove them.
end

-------------------------------------------------------------------------------
--	  Function: SWEP:PrimaryAttack()
-- Description: This function is called when the weapon's owner presses attack.
-------------------------------------------------------------------------------
function SWEP:PrimaryAttack()
	-- All shooting-related stuff is now in SWEP:Think.
end

-------------------------------------------------------------------------------
--	  Function: SWEP:SecondaryAttack()
-- Description: This function is called when the weapon's owner presses attack2
-------------------------------------------------------------------------------
function SWEP:SecondaryAttack()
	-- Just to insure it doesn't make that clicking noise.
end

-------------------------------------------------------------------------------
--	  Function: SWEP:Shoot( number )
-- Description: This function is called to create and fire knives.
--		 Input: number - This should be a int specifying the number of shots to
--					fire.
-------------------------------------------------------------------------------
function SWEP:Shoot( number )
	-- Make noise
	self.Owner:EmitSound("sakuya/knifeThrow.wav", 100, 100)
	
	-- This is the farthest left bullet's position.
	--local farleft = (- (number - 1) / 2) * self.spreaddis
	
	local tex = "models/danmaku/sakuya/BKnife"
	local color = Color(0, 0, 127)
	if (self.cardActive != 0)
	then
		tex = "models/danmaku/sakuya/GKnife"
		color = Color(0, 127, 0)
	end
	
	for i = 0, (number - 1), 1
	do
		-- Set up the projectile.
		local proj = ents.Create("sent_proj_sakuyaknife01")
		proj.Owner = self.Owner
		
		-- Find the position to shoot the knife from.
		--local offsetdis = self.Owner:GetAimVector():Angle():Right()
		--offsetdis:Normalize()
		--offsetdis = offsetdis * (farleft + (self.spreaddis * i))
		proj:SetPos(self.Owner:GetShootPos())-- + offsetdis)
		
		-- Find the angle at which to shoot the knife.
		local offsetang = (self.spreadang / 2) - ((i / (number - 1)) * self.spreadang)
		local aimvector = self.Owner:GetAimVector()
		aimvector:Rotate(Angle(0, offsetang, 0))
		aimvector:Normalize()
		proj:SetAngles(aimvector:Angle())
		
		-- Spawn it.
		proj:Spawn()
		
		-- Set some physics properties.
		proj:GetPhysicsObject():Wake()
		proj:GetPhysicsObject():EnableDrag(false)
		proj:GetPhysicsObject():EnableGravity(false)
		proj:GetPhysicsObject():SetVelocity(aimvector * self.shotspeed)
		proj:GetPhysicsObject():SetBuoyancyRatio(0)
		
		util.SpriteTrail(proj, 0, color, true, 8, 2, 0.5, 0.05, "trails/sakuya/knifeTrail.vmt")
		proj:SetMaterial(tex)
		
		-- If spellcard 3 is active, shorten the knife's timer.
		if (self.cardActive != 0)
		then
			proj.timer = CurTime() + 7.5
		else
			proj.pPerHit = self.pPerHit
			proj.pPerKill = self.pPerKill
		end
		
		-- If timestop is on, add this entity to the timestop table.
		if (sakuyaTimeStop.On)
		then
			sakuyaTimeStop.AddEntity(proj)
		end
	end
end

-------------------------------------------------------------------------------
--	  Function: SWEP:ShootWave( number )
-- Description: This function is called to fire a round wave of knives.
--		 Input: number - An integer specifying the number of knives in the wave.
-------------------------------------------------------------------------------
function SWEP:ShootWave( number )
	-- Make noise.
	self.Owner:EmitSound("sakuya/knifeThrow02.wav", 100, 100)
	
	-- Set up the projectile aim vector, and rotate it by an initial value.
	local posOffset = Vector(16, 0, 0)
	posOffset:Rotate(Angle(0, math.random(0, 359), 0))
	
	for i = 0, (number - 1), 1
	do
		-- Set up the projectile.
		local proj = ents.Create("sent_proj_sakuyaknife01")
		proj.Owner = self.Owner
		
		-- Find the position to shoot the projectile from.
		local offsetAngle = 360 * (1 / number)
		posOffset:Rotate(Angle(0, offsetAngle, 0))
		local pos = posOffset
		--pos:Rotate(Angle(0, self.Owner:GetAimVector():Angle().y, 0))
		proj:SetPos(self.Owner:GetShootPos() + pos - Vector(0, 0, 16))
		
		-- Set the projectile's angle.
		proj:SetAngles(pos:Angle())
		
		-- Spawn it.
		proj:Spawn()
		
		-- Set some physics properties.
		proj:GetPhysicsObject():Wake()
		proj:GetPhysicsObject():EnableDrag(false)
		proj:GetPhysicsObject():EnableGravity(false)
		proj:GetPhysicsObject():SetVelocity(pos:GetNormalized() * self.shotspeed)
		proj:GetPhysicsObject():SetBuoyancyRatio(0)
		
		util.SpriteTrail(proj, 0, Color(0, 32, 192, 200), true, 8, 2, 0.5, 0.05, "trails/sakuya/knifeTrail.vmt")
		proj:SetMaterial("models/danmaku/sakuya/BKnife")
		
		proj.timer = CurTime() + 7.5
		proj.pPerHit = 0
		proj.pPerKill = 0
	end
end

-------------------------------------------------------------------------------
--	  Function: SWEP:ShootDelayedWave( number )
-- Description: This function is called to fire a round wave of knives.
--		 Input: number - An integer specifying the number of knives in the wave.
-------------------------------------------------------------------------------
function SWEP:ShootDelayedWave( number )
	-- Make noise.
	self.Owner:EmitSound("sakuya/knifeThrow.wav", 100, 100)
	
	-- Set up the projectile aim vector, and rotate it by an initial value.
	local posOffset = Vector(16, 0, 0)
	posOffset:Rotate(Angle(0, math.random(0, 359), 0))
	
	for i = 0, (number - 1), 1
	do
		-- Set up the projectile.
		local proj = ents.Create("sent_proj_sakuyaknife02")
		proj.Owner = self.Owner
		
		-- Find the position to shoot the projectile from.
		local offsetAngle = 360 * (1 / number)
		posOffset:Rotate(Angle(0, offsetAngle, 0))
		local pos = posOffset
		proj:SetPos(self.Owner:GetShootPos() + pos - Vector(0, 0, 16))
		
		-- Set the projectile's angle.
		proj:SetAngles(pos:Angle())
		
		-- Spawn it.
		proj:Spawn()
		
		-- Set some physics properties.
		proj:GetPhysicsObject():Wake()
		proj:GetPhysicsObject():EnableDrag(false)
		proj:GetPhysicsObject():EnableGravity(false)
		proj:GetPhysicsObject():SetVelocity((pos + Vector(0, 0, 32)):GetNormalized() * self.sc2shotspeed)
		proj:GetPhysicsObject():SetBuoyancyRatio(0)
		
		proj:SetMaterial("models/danmaku/sakuya/RKnife2")
		
		proj.timer = CurTime() + (math.random() * 1) + 1
		proj.pPerHit = 0
		proj.pPerKill = 0
	end
end

-------------------------------------------------------------------------------
--	  Function: SWEP:AddPower( number )
-- Description: This function adds the given number to the spirit power.
-------------------------------------------------------------------------------
function SWEP:AddPower( number )
	-- If a spellcard (other than 3) is active, return early.
	if (self.cardActive == 1 || self.cardActive == 2)
	then
		return
	end

	-- Otherwise, add the amount of power to the meter.
	local pow = self:GetNetworkedFloat("spiritPower")
	pow = pow + number
	if (pow < 0) then
		pow = 0
	end
	if (pow > 100) then
		pow = 100
	end
	self:SetNetworkedFloat("spiritPower", pow)
end

-------------------------------------------------------------------------------
--	  Function: SWEP:Think()
-- Description: This function is called every step.
-------------------------------------------------------------------------------
function SWEP:Think()
	-- If a spellcard is active, check the timer.
	if (self.cardActive != 0)
	then
		local timer = self:GetNetworkedInt("spellcardTimer")
		local timeleft = math.Round(timer - CurTime())
		-- If the timer is getting close to expiring, play a sound for each second.
		if (timeleft < 11 && timeleft >= 1)
		then
			-- If timeout is 0, play a sound and record the time it was played.
			if (self.scTimeout == 0)
			then
				self:EmitSound("common/timeout.wav", 100, 100)
				self.scTimeout = CurTime()
			-- If timeout isn't 0, and we've passed the time recorded plus 1 second, reset it.
			elseif (CurTime() >= self.scTimeout + 1)
			then
				self.scTimeout = 0
			end
		end
		
		-- If the timer has expired, end the spellcard.
		if (CurTime() > timer)
		then
			-- If the player has not scored any kills, kill the player.
			if (self.Owner:Frags() - self.scKills <= 0)
			then
				self.Owner:Kill()
			-- Otherwise, just deactivate the spellcard.
			else
				-- If the spellcard was number 3, clear the spirit bar again.
				if (self.cardActive == 3)
				then
					self:AddPower( -100 )
					self.pressed = 0
				end
				self:SetNWInt("scactive", 0)
				
				-- Deactivate the spellcard.
				self.cardActive = 0
				self.shoottime = 0
				
				self.scTimeout = 0
				self.sc1Shots = 0
			end
			
			-- If time was frozen, start it again.
			if ((self.sc3timer != 0) || sakuyaTimeStop.On)
			then
				self.sc3timer = 0
				sakuyaTimeStop.Stop(self.Owner)
			end
		end
	end
	
	-- If a card is inactive, use the normal attack.
	if (self.cardActive == 0)
	then
		-- Use the basic attack.
		self:BasicAttack()

	-- However, if the spellcard is active, use a different attack.
	elseif (self.cardActive == 1)
	then
		-- Use the first spellcard attack.
		self:SpellCard1()

	elseif (self.cardActive == 2)
	then
		-- Use the second spellcard attack.
		self:SpellCard2()
	elseif (self.cardActive == 3)
	then
		-- Use the third spellcard attack.
		self:SpellCard3()	
	end
end

-------------------------------------------------------------------------------
--	  Function: SWEP:BasicAttack()
-- Description: This function is called to handle the basic primary attack.
-------------------------------------------------------------------------------
function SWEP:BasicAttack()
	-- If we can shoot, do so.
	if (self.shoottime == 0)
	then
		-- Record the time that the player pressed ATTACK.
		if (self.Owner:KeyPressed(IN_ATTACK))
		then
			self.pressed = CurTime()
		end

		-- If the player has pressed ATTACK, go ahead and see if we can shoot yet.
		if (self.pressed != 0)
		then
			-- Find out if we're fullycharged yet.
			local fullycharged = ((CurTime() > self.pressed + self.fullcharge))
			
			-- If the player releases ATTACK or we're fully charged, throw some knives.
			if (self.Owner:KeyReleased(IN_ATTACK) || fullycharged)
			then
				-- If we had full charge, fire a large burst.
				if (fullycharged)
				then
					self.Owner:EmitSound("common/charged.wav", 100, 100)
					
					timer.Simple(self.fullcharge, self.Shoot, self, self.full)
					timer.Simple(self.fullcharge, self.AddPower, self, (self.pPerShot * self.full))
					-- Begin cooldown cycle.
					self.shoottime = CurTime() + self.cooldown + self.fullcharge
				-- Otherwise, fire a small burst.
				else
					self:Shoot(self.empty)
					self:AddPower(self.pPerShot * self.empty)
					-- Begin cooldown cycle.
					self.shoottime = CurTime() + self.cooldown
				end
				
				self:SendWeaponAnim(ACT_VM_MISSCENTER)
				self.Owner:SetAnimation(PLAYER_ATTACK1)
				
				-- Reset the pressed thing to zero.
				self.pressed = 0
				
			end
		end
		
		-- If the player presses ATTACK2 (and shoottime and pressed are still 0), activate a spellcard.
		if (self.Owner:KeyPressed(IN_ATTACK2) && self.shoottime == 0 && self.pressed == 0)
		then
			local power = self:GetNetworkedFloat("spiritPower")
			local alertMessage = ""
			-- If the player has filled only the first bar, use spellcard number 1.
			if (power >= 33 && power < 66)
			then
				self:SetNWInt("scactive", 1)
				self:AddPower ( -power )
				self.cardActive = 1
				alertMessage = "\"Silver Bound\""
			-- If the player has filled the first two bars, use spellcard number 2.
			elseif (power >= 66 && power < 100)
			then
				self:SetNWInt("scactive", 2)
				self:AddPower ( -power )
				self.cardActive = 2
				alertMessage = "\"Killing Doll\""
			-- If the player has filled all three bars, use spellcard number 3.
			elseif (power >= 100)
			then
				self:SetNWInt("scactive", 3)
				self:AddPower ( -power )
				self.cardActive = 3
				alertMessage = "\"The World\""
			end
			
			-- If we activated a spellcard, notify all players, including the user himself.
			if (self.cardActive != 0)
			then
				self.shoottime = 0
				self.Owner:EmitSound("common/spellcardBig.wav", 100, 100)
				
				-- Make a spellcard use effect.
				local edata = EffectData()
				edata:SetStart(self.Owner:GetShootPos())
				edata:SetOrigin(self.Owner:GetShootPos())
				edata:SetScale(1)
				util.Effect("sprite_spellcardUse", edata)
				
				-- Alert the user that he has activated a spellcard.
				self.Owner:PrintMessage(HUD_PRINTCENTER, "Spellcard activated: " .. alertMessage)
				-- Alert the other players that a spellcard has been activated.
				for i, v in pairs (player.GetAll())
				do
					-- If this player isn't the one who activated the spellcard, send the message.
					if (v != self.Owner)
					then
						v:PrintMessage(HUD_PRINTCENTER, self.Owner:Nick() .. " activated spellcard: " .. alertMessage)
					end
				end
				-- Also, set the spellcard timer.
				self:SetNetworkedInt("spellcardTimer", CurTime() + self.scTimer)
				
				-- And set the kills number
				self.scKills = self.Owner:Frags()		

				-- ...And reset pressed. Just to make sure.
				self.pressed = 0
			end
		end
	-- If we can't shoot, but the shoottime has been passed, reset the shoottime (so we can shoot again).
	elseif (self.shoottime < CurTime())
	then
		self.shoottime = 0
	end
end

-------------------------------------------------------------------------------
--	  Function: SWEP:SpellCard1()
-- Description: This function is called to handle the first spellcard's attack.
-------------------------------------------------------------------------------
function SWEP:SpellCard1()
	-- If we can shoot, do so.
	if (self.shoottime == 0)
	then
		-- If the player is holding ATTACK, fire of several bursts of shots.
		if (self.Owner:KeyDown(IN_ATTACK))
		then
			-- If we still have some waves left, fire a wave of shots.
			if (self.sc1Shots < self.sc1MaxShots)
			then
				-- Based on the player's health, fire a wave with a certain number of shots.
				local playerHP = self.Owner:Health()
				-- Easy mode
				if (playerHP > 50)
				then
					self:ShootWave(6)
				-- Normal mode
				elseif (playerHP > 25)
				then
					self:ShootWave(10)
				-- Hard mode
				elseif (playerHP > 10)
				then
					self:ShootWave(16)
				-- Lunatic
				else
					self:ShootWave(24)
				end
				
				self:SendWeaponAnim(ACT_VM_MISSCENTER)
				self.Owner:SetAnimation(PLAYER_ATTACK1)
				
				-- Set the shoottime timer.
				self.shoottime = CurTime() + self.sc1shottime
				
				-- Increment sc1Shots
				self.sc1Shots = self.sc1Shots + 1
			end
		end
	-- If we can't shoot and shoottime has been passed, reset shoottime.
	elseif (self.shoottime < CurTime())
		then
			self.shoottime = 0
	end
	
	-- If the player releases ATTACK, reset the wave counter and set shoottime.
	if (self.Owner:KeyReleased(IN_ATTACK))
	then
		self.sc1Shots = 0
		self.shoottime = CurTime() + self.sc1cooldown
	end
end

-------------------------------------------------------------------------------
--	  Function: SWEP:SpellCard2()
-- Description: This function is called to handle the second spellcard's attack
-------------------------------------------------------------------------------
function SWEP:SpellCard2()
	-- If we can shoot, do so.
	if (self.shoottime == 0)
	then
		-- If the player presses ATTACK, fire a wave of delayed shots.
		if (self.Owner:KeyPressed(IN_ATTACK))
		then
			-- Based on the player's health, fire a wave with a certain number of shots.
			local playerHP = self.Owner:Health()
			-- Easy mode
			if (playerHP > 50)
			then
				self:ShootDelayedWave(8)
			-- Normal mode
			elseif (playerHP > 25)
			then
				self:ShootDelayedWave(12)
			-- Hard mode
			elseif (playerHP > 10)
			then
				self:ShootDelayedWave(18)
			-- Lunatic
			else
				self:ShootDelayedWave(26)
			end
			
			self:SendWeaponAnim(ACT_VM_MISSCENTER)
			self.Owner:SetAnimation(PLAYER_ATTACK1)
			
			-- Set the shoottime timer.
			self.shoottime = CurTime() + self.sc2cooldown
		end
	
	-- If we can't shoot, do other stuff.
	else
		-- If shoottime has been passed, reset shoottime.
		if (self.shoottime < CurTime())
		then
			self.shoottime = 0
		end
	end
end

-------------------------------------------------------------------------------
--	  Function: SWEP:SpellCard3()
-- Description: This function is called to handle the third spellcard's attack.
-------------------------------------------------------------------------------
function SWEP:SpellCard3()
	-- If we can shoot, do so.
	if (self.shoottime == 0)
	then
		-- If the player presses ATTACK and we aren't charging a time freeze, fire a wave of delayed shots.
		if (self.Owner:KeyPressed(IN_ATTACK) && self.pressed == 0)
		then
			-- Based on the player's health, fire a sweep of knives (like in the normal attack)
			local playerHP = self.Owner:Health()
			-- Easy mode
			if (playerHP > 50)
			then
				self:Shoot(3)
			-- Normal mode
			elseif (playerHP > 25)
			then
				self:Shoot(5)
			-- Hard mode
			elseif (playerHP > 10)
			then
				self:Shoot(7)
			-- Lunatic
			else
				self:Shoot(9)
			end
			
			self:SendWeaponAnim(ACT_VM_MISSCENTER)
			self.Owner:SetAnimation(PLAYER_ATTACK1)
			
			-- Set the shoottime timer.
			self.shoottime = CurTime() + self.sc3cooldown			
		end
	
		-- Shoottime is still 0 and time isn't stopped, handle ATTACK2-related stuff.
		if ((self.shoottime == 0) && !sakuyaTimeStop.On)
		then
			-- Record the time at which the player first presses ATTACK2.
			if (self.Owner:KeyPressed(IN_ATTACK2))
			then
				self.pressed = CurTime()
			end
			
			-- If the player has pressed ATTACK2, go ahead and see if we can "shoot" yet.
			if (self.pressed != 0)
			then
				-- Find out if we're fullycharged yet.
				local fullycharged = ((CurTime() > self.pressed + self.sc3charge))
				
				-- If the player releases ATTACK2 or we're fully charged, do stuff.
				if (self.Owner:KeyReleased(IN_ATTACK2) || fullycharged)
				then
					-- If we had full charge, freeze time for a short time.
					if (fullycharged)
					then
						self.Owner:EmitSound("sakuya/timeStop.wav", 100, 100)
						-- Freeze time!
						sakuyaTimeStop.Start(self.Owner)
						
						-- Set the timer.
						self.sc3timer = CurTime() + self.sc3timertime
						
						-- Begin cooldown cycle.
						self.shoottime = CurTime() + self.cooldown
					-- If we weren't charged, just cost some cooldown.
					else
						-- Begin cooldown cycle.
						self.shoottime = CurTime() + self.sc3cooldown
					end
					-- Reset the pressed thing to zero.
					self.pressed = 0
					
				end
			end
		end
	-- If we can't shoot, do other stuff.
	else
		-- If shoottime has been passed, reset shoottime.
		if (self.shoottime < CurTime())
		then
			self.shoottime = 0
		end
	end
	
	-- If the timer is on, or time is stopped, check if we need to start time again.
	if ((self.sc3timer != 0) && sakuyaTimeStop.On)
	then
		-- If the timer has been passed, start time again.
		if (self.sc3timer < CurTime())
		then
			self.sc3timer = 0
			sakuyaTimeStop.Stop(self.Owner)
		end
	end
	
	-- If the player is holding attack2, set the spirit power to correspond with the amount of charge.
	if (self.pressed != 0)
	then
		self:SetNWFloat("spiritPower", 100 * ((CurTime() - self.pressed) / self.sc3charge))
	-- Otherwise, set the spirit power to either 0 or 100, depending on whether we've stopped time.
	else
		-- If the timefreeze timer is active, set spiritpower to 100.
		if (self.sc3timer != 0)
		then
			if (self:GetNWFloat("spiritPower") != 100)
			then
				self:SetNWFloat("spiritPower", 100)
			end
		-- If the timefreeze timer is inactive, set spiritpower to 0.
		elseif (self:GetNWFloat("spiritPower") != 0)
		then
			self:SetNWFloat("spiritPower", 0)
		end
	end
	
end

-------------------------------------------------------------------------------
--	  Function: SWEP:NPCShoot_Primary(ShootPos, ShootDir)
-- Description: This function is called when an NPC tries to use the weapon.
-------------------------------------------------------------------------------
--[[function SWEP:NPCShoot_Primary(ShootPos, ShootDir)
	-- Make noise.
	self.Owner:EmitSound("sakuya/knifeThrow.wav", 100, 100)
	
	local proj = ents.Create("sent_proj_sakuyaknife01")
	--proj:SetModel("models/weapons/W_missile_launch.mdl")
	proj:SetPos(ShootPos)
	proj:SetAngles(ShootDir:Angle())
	proj:SetOwner(self.Owner)
	
	proj:Spawn()
	
	proj:GetPhysicsObject():Wake()
	proj:GetPhysicsObject():EnableDrag(false)
	proj:GetPhysicsObject():EnableGravity(false)
	proj:GetPhysicsObject():SetVelocity(ShootDir * self.shotspeed)
	proj:GetPhysicsObject():SetBuoyancyRatio(0)
	
end]]

-------------------------------------------------------------------------------
--	  Function: Console Command SakuyaAddSpirit
-- Description: Adds the spellcard equivalent of the provided number to the
--					spirit power meter.
-------------------------------------------------------------------------------
concommand.Add("SakuyaAddSpirit", 
	function (ply, cmd, args)
		-- If the player doesn't have the SWEP or he's not an admin, return early.
		if (!ply:HasWeapon("weapon_sakuya") || !ply:IsAdmin())
		then
			return
		end
		
		-- This is the number to add to the spirit power meter.
		local power = 0
		
		-- If the player doesn't provide the argument, just assume full.
		if (!args[1])
		then
			power = 100
		-- Otherwise, muliply the argument by 100 / 3 and store it in power.
		else
			power = tonumber(args[1]) * 34
		end
		
		-- Call add power on the player's swep.
		local wep = ply:GetWeapon("weapon_sakuya")
			if (wep && wep:IsValid())
			then
				wep:AddPower(power)
			end
	end
)